package day1206zy.day1206zy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class demo4 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> stu = Class.forName("day1206zy.Student");
        Method c = stu.getDeclaredMethod("show");
        c.setAccessible(true);
//        获取带参构造方法
        Constructor<?> c1 = stu.getDeclaredConstructor(String.class,int.class);
//        设置类外可见
        c1.setAccessible(false);
//       false因为构造方法用的public，如果是private，将会报错
        Object o = c1.newInstance("sdf",5);
//        newInstances==new Student
        Object result = c.invoke(o);
        System.out.println(result);
    }
}
